import React, { useState, useEffect } from 'react';
import { View } from 'react-native';
import { Toast } from '@ant-design/react-native';
import AddButton from '../../components/AddButton';
import ImpTaskList from '../../components/ImpTaskView';
import { windowWidth } from '../../comconfig/screen';
import { selectImptask, sortImptask } from '../../comconfig/sqlfun';

const ImportantWork = ({ navigation }) => {
    const [impTaskList, setImpTaskList] = useState([]);// 通过useState声明变量
    const getList = async () => {
        try {
            const list = await selectImptask();// 通过异步函数得到接口的返回值
            const arr = [];// 声明一个数组备用
            for (let i = 0; i < list.length; i += 1) {
                arr.push(JSON.parse(list[i]));// 将得到的数据进行格式化
            }
            setImpTaskList(arr);// 将得到的数据存储在声明的变量中
        } catch (error) {
            Toast.info('获取数据失败', 1, undefined, false);
        }
    };
    const sortList = async (type) => {
        try {
            const list = await sortImptask(type);
            const arr = [];
            for (let i = 0; i < list.length; i += 1) {
                arr.push(JSON.parse(list[i]));
            }
            setImpTaskList(arr);
        } catch (error) {
            Toast.info('获取数据失败', 1, undefined, false);
        }
    };
    useEffect(() => {
        getList();
    }, []);
    return (
        <View style={{ alignItems: 'center', flex: 1 }}>
            {/* <Button
            type="primary"
            onPress={() => navigation.navigate('添加事务')}
        >
            添加事务
        </Button> */}
            <View style={{ marginTop: 10, marginBottom: 10, width: windowWidth }}>
                <ImpTaskList
                    navigation={navigation}
                    impTaskList={impTaskList}
                    getList={getList}
                    sortList={sortList}
                />
            </View>
            <AddButton navigation={navigation} getList={getList} action="添加事务" />
        </View>
    );
};
export default ImportantWork;
